/**
 * 
 */
package com.taobao.wolfwolf.hibernate.dao.impl;

import java.util.List;


import com.taobao.wolfwolf.hibernate.dao.SpecsDao;
import com.taobao.wolfwolf.hibernate.pojo.Specs;

/**
 * @author Tux
 *
 * 创建时间: 2011-5-19 上午10:22:07
 */
@SuppressWarnings("unchecked")
public class SpecsDaoImpl extends BaseDaoImpl implements SpecsDao {

	public Specs findById(int id) throws Exception {
		return (Specs) getHibernateTemplate().get(Specs.class, id);
	}
	
	public List<Specs> findByName(String name) throws Exception {
		return super.findByProperty(Specs.class, "name", name);
	}

	public List<Specs> findAll() throws Exception {
		return getHibernateTemplate().find("from Specs");
	}

	public long getTotal() throws Exception {
		return super.countByCriteria(Specs.class);
	}

	public List<Specs> paging(int page, int limit) throws Exception {
		return super.findByCriteriaByPage(Specs.class, page, limit);
	}

	//TODO 混乱的代码
	public int hasEntity(Specs specs) {
		
		String hql = "from Specs as sp where sp.name = ? and sp.price = ?";
		List list = this.getHibernateTemplate().find(hql, specs.getName(), specs.getPrice());
		
		return list.isEmpty() ? -1 : ((Specs)list.get(0)).getId();
	}
}
